package com.mikhailenko.transportMVC.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.mikhailenko.transportMVC.exeption.GroupNotFoundExeption;
import com.mikhailenko.transportMVC.model.Group;
import com.mikhailenko.transportMVC.service.GroupService;

@Controller
@RequestMapping(value = "/group")
public class GroupController {

    @Autowired
    private GroupService groupService; 
    
    @RequestMapping(value = "create", method = RequestMethod.GET)
    public ModelAndView createGroupPage(){
        ModelAndView mav = new ModelAndView("group-new", "group", new Group());
        return mav;
    } 
    
    @RequestMapping(value = "create", method = RequestMethod.POST)
    public ModelAndView createNewGroup(@ModelAttribute Group group,
            BindingResult result,
            final RedirectAttributes redirectAttributes){
        ModelAndView mav = new ModelAndView();
        
        groupService.create(group);
        String message = "New user " + group.getName()
                + " was successfully created.";
        
        mav.setViewName("redirect:/group/list");
        
        redirectAttributes.addFlashAttribute("message", message);
        return mav;
    }
    
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ModelAndView viewGroupList(){
        ModelAndView mav = new ModelAndView("group-list");
        List<Group> groups = groupService.findAll();
        mav.addObject("groups", groups);
        return mav;
    }
    
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public ModelAndView editGroupPage(@PathVariable Integer id) {
        ModelAndView mav = new ModelAndView("group-edit");
        Group group = groupService.findById(id);
        mav.addObject("group", group);
        return mav;
    }

    @RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
    public ModelAndView editUser(@ModelAttribute Group group,
            BindingResult result,
            @PathVariable Integer id,
            final RedirectAttributes redirectAttributes)
            throws GroupNotFoundExeption {


        
        groupService.update(group);

        ModelAndView mav = new ModelAndView("redirect:/group-list");
        String message = "User:"+group.getName()+" was successfully updated.";

        redirectAttributes.addFlashAttribute("message", message);
        return mav;
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public ModelAndView deleteUser(@PathVariable Integer id,
            final RedirectAttributes redirectAttributes)
            throws GroupNotFoundExeption {

        ModelAndView mav = new ModelAndView("redirect:/index.jpa");

        Group group = groupService.delete(id);
        String message = "The user " + group.getName()
                + " was successfully deleted.";

        redirectAttributes.addFlashAttribute("message", message);
        return mav;
    }
}
